package edu.gatech.cs2340.sharks.fms;

/**
 * ViewUserActivity.java
 * @author Miri Baker
 * 
 * Shows a user's profile information and status.
 * Allows an admin to lock/unlock this account.
 * Allows an admin to make this user another admin.
 */


import edu.gatech.cs2340.sharks.fms.data.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class ViewUserActivity extends Activity
{
	/**
	 * the user whose profile is being viewed
	 */
	private User user;
	
	/**
	 * ClickListener for the status change button
	 */
	private OnClickListener statusClick;
	
	/**
	 * TextView showing the user's lock status
	 */
	private TextView statusView;
	
	/**
	 * reference to status change button
	 */
	private Button btn;
	
	/**
	 * The current instance of the activity, used to make sure there is only one instance of this activity running at a time.
	 */
	private static Activity instance = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_view_user);
		
		if (instance != null) instance.finish();
		instance = this;

		Intent intent = getIntent();

		String userID = intent.getStringExtra("userID");

		/**
		 * Gets the user packet of this profile's user.
		 * If packet status code is not OK, 
		 * throws an alert dialog. 
		 */
		Packet<User> pu = DBMgr.getUser(userID);

		if (pu.status.code() != Status.Code.OK) Info.alert(this, pu.status.toString());
		else
		{
			user = pu.value;

			// Gets TextViews from the layout to be set.
			TextView handleView = (TextView) findViewById(R.id.textviewHandle);
			TextView nameView = (TextView) findViewById(R.id.textviewName);
			TextView locView = (TextView) findViewById(R.id.textviewLocation);
			TextView profileView = (TextView) findViewById(R.id.textViewProfile);
			TextView emailView = (TextView) findViewById(R.id.textviewEmail);
			TextView roleView = (TextView) findViewById(R.id.textviewRole);
			statusView = (TextView) findViewById(R.id.textviewStatus);

			// Sets text views to display user info.
			handleView.setText(user.handle());
			nameView.setText(user.name);
			locView.setText(user.location);
			profileView.setText(user.profile);
			emailView.setText(user.email);

			if (user.role() == User.Role.ADMIN) roleView.setText(R.string.role_admin);
			else roleView.setText(R.string.role_client);

			// Gets button from layout.
			btn = (Button) findViewById(R.id.buttonChangeStatus);

			// ClickListener that checks a user's locked status
			// and sets the button click method appropriately.
			statusClick = new OnClickListener()
			{
				public void onClick(View v)
				{
					if(user.valid) lockUser(v);
					else unlockUser(v);
				}
			};
			
			// Sets click listener for button.
			// Sets text contingent upon whether the user is
			// locked or unlocked. 
			btn.setOnClickListener(statusClick);
			if (!user.valid)
			{
				statusView.setText(R.string.user_locked);
				btn.setText(R.string.button_unlock);
			}
			else
			{
				statusView.setText(R.string.user_unlocked);
				btn.setText(R.string.button_lock);
			}

		}

	}

	/**
	 * Unlocks a locked user when the button is pressed. Enables "lock" button.
	 * Disables "unlock" button.
	 * 
	 * @param view The current view
	 */
	public void unlockUser(View view)
	{
		Status s = DBMgr.unlockUser(DBMgr.user, this.user);
		if (s.code() != Status.Code.OK) Info.alert(this, s.toString());
		// Display popup "User has been unlocked."; allow positive button
		updateButtons(view);
	}

	/**
	 * Locks an unlocked user when the button is pressed. Enables "unlock"
	 * button. Disables "lock" button.
	 * 
	 * @param view
	 */
	public void lockUser(View view)
	{
		Status s = DBMgr.lockUser(DBMgr.user, this.user);
		if (s.code() != Status.Code.OK) Info.alert(this, s.toString());
		// Display popup "User has been locked."; allow positive button
		updateButtons(view);
	}

	/**
	 * Removes this user from application database.
	 * @param view
	 */
	public void deleteUser(View view) // TODO Check certainty and display popups
	{
		// Display popup "Are you sure you wish to delete this user?";
		// allow positive and negative buttons.
		// If negative: finish();
		// If positive:
		DBMgr.deleteUser(DBMgr.user, this.user);
		Intent toHome = new Intent(this, AdminHomeActivity.class);
		startActivity(toHome); // Starting new intent required to update list of users
		finish();
		// Display popup "User has been deleted."; finish() on button

	}

	/**
	 * Updates status text, button functionality, and button text.
	 * 
	 * @param view
	 */
	public void updateButtons(View view)
	{
		if (!user.valid)
		{
			statusView.setText(R.string.user_locked);
			btn.setText(R.string.button_unlock);
		}
		else
		{
			statusView.setText(R.string.user_unlocked);
			btn.setText(R.string.button_lock);
		}
	}
}